<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="设备编码" prop="deviceCode">
        <el-input v-model="queryParams.deviceCode" placeholder="请输入设备编码" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="故障名称" prop="faultName">
        <el-input v-model="queryParams.faultName" placeholder="请输入故障名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" type="index" width="50">
      </el-table-column>
      <el-table-column label="设备编码" prop="deviceCode" width="auto"/>
      <el-table-column label="设备状态" prop="deviceStatus" :show-overflow-tooltip="true" width="auto" >
        <template slot-scope="scope">
          <span>{{ statusList[scope.row.deviceStatus]}}</span>
        </template>
      </el-table-column>
      <el-table-column label="故障代码" prop="faultCode" :show-overflow-tooltip="true" width="auto" />
      <el-table-column label="故障名称" prop="faultName" :show-overflow-tooltip="true" width="auto" />
      <el-table-column label="报警状态" prop="faultStatus" :show-overflow-tooltip="true" width="auto">
        <template slot-scope="scope">
          <span>{{ scope.row.faultStatus == '0'? '否':'是' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="故障说明" prop="faultDescription" :show-overflow-tooltip="true" width="auto" />
      <el-table-column label="状态时间" prop="dataTime" :show-overflow-tooltip="true" width="auto" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dataTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
  import { EquipStatusQuery, EquipUseQuery } from "@/api/system/datamanage";
  import moment from 'moment'


  export default {
    name: "Role",
    // dicts: ['alarm_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 角色表格数据
        deviceList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示弹出层（数据权限）
        openDataScope: false,
        menuExpand: false,
        menuNodeAll: false,
        deptExpand: true,
        deptNodeAll: false,
        // 日期范围
        dateRange: ['', ''],
        // 菜单列表
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize:10,
          deviceCode: ''
        },
        // 表单参数
        form: {},
        statusList:{
          0:'设备离线',
          1:'开始生产',
          2:'暂停生产',
          3:'重新生产',
          4:'完工状态'
        }


      };
    },
    created() {
      let startTime = moment().subtract(1, 'month').format('YYYY-MM-DD') +' 00:00:00';
      let endTime = this.parseTime(new Date());
      this.dateRange = [startTime,endTime]
      this.getList();
    },
    methods: {
      /** 查询设备状态列表 */
      getList() {
        this.loading = true;
        console.log(9999,this.queryParams,this.addDateTimeRange(this.queryParams,this.dateRange))
        EquipStatusQuery(this.addDateTimeRange(this.queryParams,this.dateRange)).then(response => {
          this.deviceList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 取消按钮（数据权限）
      cancelDataScope() {
        this.openDataScope = false;
        this.reset();
      },
      // 表单重置
      reset() {
        if (this.$refs.menu != undefined) {
          this.$refs.menu.setCheckedKeys([]);
        }
        this.menuExpand = false,
          this.menuNodeAll = false,
          this.deptExpand = true,
          this.deptNodeAll = false,
          this.form = {
            roleId: undefined,
            roleName: undefined,
            roleKey: undefined,
            roleSort: 0,
            status: "0",
            menuIds: [],
            deptIds: [],
            menuCheckStrictly: true,
            deptCheckStrictly: true,
            remark: undefined
          };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.roleId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      // 更多操作触发
      handleCommand(command, row) {
        switch (command) {
          case "handleDataScope":
            this.handleDataScope(row);
            break;
          case "handleAuthUser":
            this.handleAuthUser(row);
            break;
          default:
            break;
        }
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.roleId != undefined) {
              this.form.menuIds = this.getMenuAllCheckedKeys();
              updateRole(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.form.menuIds = this.getMenuAllCheckedKeys();
              addRole(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 提交按钮（数据权限） */
      submitDataScope: function () {
        if (this.form.roleId != undefined) {
          this.form.deptIds = this.getDeptAllCheckedKeys();
          dataScope(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.openDataScope = false;
            this.getList();
          });
        }
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const roleIds = row.roleId || this.ids;
        this.$modal.confirm('是否确认删除角色编号为"' + roleIds + '"的数据项？').then(function () {
          return delRole(roleIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/role/export', {
          ...this.queryParams
        }, `role_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
